package com.kystar.ledcontrol.recycleview.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.kystar.ledcontrol.recycleview.BaseApp;
import com.kystar.ledcontrol.recycleview.R;
import com.kystar.ledcontrol.recycleview.adapter.BaseQuickAdapter;
import com.kystar.ledcontrol.recycleview.adapter.OnItemClickListener;
import com.kystar.ledcontrol.recycleview.adapter.QuickAdapter;
import com.kystar.ledcontrol.recycleview.adapter.VHolder;
import com.kystar.ledcontrol.recycleview.info.ImageViewInfo;
import com.kystar.ledcontrol.recycleview.util.FileManager;
import com.kystar.ledcontrol.recycleview.util.GsonUtil;
import com.kystar.ledcontrol.recycleview.util.TLog;

import java.util.List;

public class ImagePickerActivity extends AppCompatActivity {
    public static final int IMAGE = 1;
    public static final String INDEX = "position";
    public static final String PARAM = "imageDir";
    RecyclerView mRecyclerView;
    QuickAdapter<String> mAdapter;
    List<String> mList;
    GridLayoutManager mManager;
    int firstPosition;
    int lastPosition;
    MyHandler handler;
    ConstraintLayout mLayout;
    private BaseApp mAPP = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_picker);
        mRecyclerView = findViewById(R.id.recycleView);
        mLayout = findViewById(R.id.layout);
        String dirImage = getIntent().getStringExtra(MainActivity.PARAM);
        mList = FileManager.getImgListByDir(dirImage);
        mAdapter = new QuickAdapter<String>(R.layout.item_image,mList) {
            @Override
            protected void convert(VHolder holder, String s) {
                Glide.with(ImagePickerActivity.this).load(s).into((ImageView) holder.getView(R.id.image));
            }
        };
        handler = new MyHandler();
        mAPP = BaseApp.getApp();
        mAPP.setHandler(handler);
        mManager = new GridLayoutManager(this,4);
        mRecyclerView.setLayoutManager(mManager);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @SuppressLint("ResourceAsColor")
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                int[] positions = new int[3];
                ImageViewInfo imageViewInfo = new ImageViewInfo();
                imageViewInfo.setImageWidth(view.getWidth());
                imageViewInfo.setImageHeight(view.getHeight());
                imageViewInfo.setX(view.getLeft());
                imageViewInfo.setY(view.getTop());
                imageViewInfo.setCenter_x(view.getLeft()+view.getWidth()/2);
                imageViewInfo.setCenter_y(view.getTop()+view.getHeight()/2);
                TLog.e("left",view.getLeft(),"top",view.getTop());
                Intent intent = new Intent(ImagePickerActivity.this,PerViewActivity.class);
                String s = GsonUtil.defaultGson().toJson(mList);
                firstPosition = mManager.findFirstCompletelyVisibleItemPosition();
                lastPosition = mManager.findLastCompletelyVisibleItemPosition();
                positions[0] = position;
                positions[1] = firstPosition;
                positions[2] = lastPosition;
                intent.putExtra(ImagePickerActivity.PARAM,s);
                intent.putExtra("imageInfo",imageViewInfo);
                intent.putExtra(INDEX,positions);
//                Bitmap bitmap = SaveBitmap.loadBitmapFromView(mRecyclerView);
//                SaveBitmap.setBitmap(bitmap);
                startActivityForResult(intent,IMAGE);
            }
        });

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

//        if(requestCode == IMAGE){
//            if(resultCode == RESULT_OK){
//                boolean isScroll = data.getBooleanExtra("isScroll",false);
//              //  int position = data.getIntExtra("position",0);
//                if(isScroll){
//                    mRecyclerView.scrollToPosition(position);
//                }
//            }
//        }
    }

    public final class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            TLog.e("msg",msg.arg1);
            if(msg.what == 0x0){
                mRecyclerView.scrollToPosition(msg.arg1);
            }
        }
    }
}
